<template>
  <div class="app-container" style="overflow: scroll">
    <el-form ref="submitParams" :model="submitParams" :rules="rules" label-width="100px">
      <el-row>
        <el-col :offset="6" :span="6">
          <el-form-item label="系统编号" prop="sysCode">
            <el-select
              v-model="submitParams.sysCode"
              placeholder="系统编号"
              style="width: 100%"
              @change="sysCodeChange"
              :disabled="!isshow"
            >
              <el-option
                v-for="dict in dictMap.ORGAN_SYSTYPE_310"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row type="flex">
        <el-col :offset="6" :span="6">
          <el-form-item label="报文类型" prop="msgType">
            <el-select v-model="submitParams.msgType" placeholder="报文类型" style="width: 100%" :disabled="!isshow">
              <el-option
                v-for="dict in sysCodeList"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row type="flex">
        <el-col :offset="6" :span="6">
          <el-form-item label="业务类型编码" prop="bizTypeCode">
            <el-input
              v-model="submitParams.bizTypeCode"
              placeholder="请输入业务类型编码"
              @keyup.enter.native="getbase('BIZ_TYPE')"
              :disabled="!isshow"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-input v-model="submitParams.bizTypeCodeDesc" :disabled="true" />
        </el-col>
        <el-col :span="4">
          <el-row>
            <el-button type="primary" icon="el-icon-search" plain @click="openBasedataDialog('BIZ_TYPE')">
              选择业务类型编码
            </el-button>
          </el-row>
        </el-col>
      </el-row>

      <el-row type="flex">
        <el-col :offset="6" :span="6">
          <el-form-item label="业务种类编码" prop="bizTypeCode">
            <el-input
              v-model="submitParams.bizCtgyCode"
              placeholder="请输入业务种类编码"
              @keyup.enter.native="getbase('BIZ_CTGY')"
              :disabled="!isshow"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-input v-model="submitParams.bizCtgyCodeDesc" :disabled="true" />
        </el-col>
        <el-col :span="4">
          <el-row>
            <el-button type="primary" icon="el-icon-search" plain @click="openBasedataDialog('BIZ_CTGY')">
              选择业务类型编码
            </el-button>
          </el-row>
        </el-col>
      </el-row>

      <el-row>
        <el-col :offset="6" :span="12">
          <el-form-item>
            <el-button type="primary" v-if="isshow" @click="submitForm" v-loading.fullscreen.lock="loading">
              提 交
            </el-button>
            <el-button @click="refresh">重 置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <base-data-dialog ref="basedataDialog" @confirm="confirmData"></base-data-dialog>
  </div>
</template>

<script>
import { listPageMixin } from "@/libs/listPageMixin";
import BaseDataDialog from "@/views/components/BasedataDialog";
import { getBasedata } from "@/api/cnaps/common/basedata";
import { ccmsBusinessDownloadApplyEntrySave } from "@/api/cnaps/ccms/929";

export default {
  name: "generalInformationEntry",
  mixins: [listPageMixin],
  components: { BaseDataDialog },
  computed: {},
  data() {
    return {
      sysCodeList: [],
      msgTypeList: [],
      bizTypeList: [],
      loading: false,
      // 录入参数
      submitParams: {
        rspId: undefined,
        sysCode: undefined,
        msgType: undefined,
        bizTypeCode: undefined,
        bizTypeCodeDesc: undefined,
        bizCtgyCode: undefined,
        bizCtgyCodeDesc: undefined,
      },

      //
      searchFormDictList: ["ORGAN_SYSTYPE_310"],

      isshow: true,

      rules: {
        sysCode: [{ required: true, message: "请输入系统编号", trigger: "blur" }],
        msgType: [{ required: true, message: "请输入报文类型", trigger: "blur" }],
      },
    };
  },
  mounted() {},
  methods: {
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["submitParams"].validate((valid) => {
        if (valid) {
          this.loading = true;
          ccmsBusinessDownloadApplyEntrySave(this.submitParams)
            .then((response) => {
              this.msgSuccess("提交成功");
              this.$set(this.submitParams, "rspId", response.result.rspId);
              this.isshow = false;
              this.loading = false;
            })
            .catch((err) => {
              this.loading = false;
            });
        }
      });
    },
    //点击重置按钮
    refresh() {
      this.$refs.submitParams.resetFields();
      this.submitParams.bizTypeCode = undefined;
      this.submitParams.bizTypeCodeDesc = undefined;
      this.submitParams.bizCtgyCode = undefined;
      this.submitParams.bizCtgyCodeDesc = undefined;
      this.isshow = true;
    },
    sysCodeChange(code) {
      let dictKeyPre = `MSG_TYPE148_${code}`;
      this.getDicts(dictKeyPre).then((response) => {
        console.log(response);
        this.sysCodeList = response.result;
        this.$set(this.submitParams, "msgType", undefined);
      });
    },

    openBasedataDialog(dataType = "") {
      this.$refs.basedataDialog.show(dataType);
    },

    confirmData(dataType, row) {
      console.log(row);
      if (dataType === "BIZ_TYPE") {
        this.$set(this.submitParams, "bizTypeCode", row.dataValue);
        this.$set(this.submitParams, "bizTypeCodeDesc", row.dataDesc);
      }
      if (dataType === "BIZ_CTGY") {
        this.$set(this.submitParams, "bizCtgyCode", row.dataValue);
        this.$set(this.submitParams, "bizCtgyCodeDesc", row.dataDesc);
      }
    },

    //回车获取basedata值
    getbase(dataType) {
      if (dataType === "BIZ_TYPE") {
        let dataValue = this.submitParams.bizTypeCode;
        getBasedata(dataType, dataValue)
          .then((response) => {
            if (response.code == "S" && response.result !== null) {
              this.$set(this.submitParams, "bizTypeCode", response.result.dataValue);
              this.$set(this.submitParams, "bizTypeCodeDesc", response.result.dataDesc);
            } else {
              this.msgSuccess("找不到对应的字典项");
              this.$set(this.submitParams, "bizTypeCodeDesc", null);
            }
          })
          .catch((err) => {
            this.$set(this.submitParams, "bizTypeCodeDesc", null);
          });
      }

      if (dataType === "BIZ_CTGY") {
        let dataValue = this.submitParams.bizCtgyCode;
        getBasedata(dataType, dataValue)
          .then((response) => {
            if (response.code == "S" && response.result !== null) {
              this.$set(this.submitParams, "bizCtgyCode", response.result.dataValue);
              this.$set(this.submitParams, "bizCtgyCodeDesc", response.result.dataDesc);
            } else {
              this.msgSuccess("找不到对应的字典项");
              this.$set(this.submitParams, "bizCtgyCodeDesc", null);
            }
          })
          .catch((err) => {
            this.$set(this.submitParams, "bizCtgyCodeDesc", null);
          });
      }
    },
  },
};
</script>
